<?php

namespace app\services;



use app\lib\exception\ParamException;
use app\model\Airlines;
use app\model\Airports;
use app\model\City;
use app\model\OrderInfo;
use app\model\Sales;
use think\facade\Cache;

class OrderService
{
    /**
     * @var OrderInfo
     */
    private $dao;

    public function __construct()
    {
        $this->dao = new OrderInfo();

    }


//0已下单/未定位 1已定位 2已出票  3取消订单 4出票失败 6已退款 7成功 8一半定位
    public function add($user_id, $sales_type,$currency,  $origin,$destination,$remark,$seat_type, $flight_no, $fly_day, $fly_time, $sales_price, $office_no){
        if (preg_match('/\w\w/', $flight_no, $airlines)){
            $airlinesDao = new Airlines();
            $airlines = $airlinesDao->where('airlines', $airlines[0])->find()->airlines_name??'未知航司';
        }else{
            throw new ParamException('航班号有误导致无法识别航司');
        }
        $order_number = 'CT'.date('YmdHis', time()).rand(0,9).rand(0,9).rand(0,9);
//        $sales_info = Sales::where('staff_id', $sales_id)->find();
        $_fly_day = strtotime($fly_day);


        $flight_destination_from = $this->getCity($origin);
        $flight_destination_to = $this->getCity($destination);


        return $this->dao->create([
           'voyage'=>$origin.$destination,
           'flight_no'=>$flight_no,
           'sales_type'=>$sales_type,
           'currency'=>$currency,
           'flight_destination_from'=>$flight_destination_from,
           'flight_destination_to'=>$flight_destination_to,
           'booking_date'=>date('d/m/Y', time()),
           'office_no'=>$office_no,
           'user_id'=> $user_id,
           'order_number'=>$order_number,
           'fly_day'=>date('d/m/Y', $_fly_day),
           'format_fly_day'=>$_fly_day,
           'fly_time'=> $fly_time,
           'fnancial_type'=>1,
           'order_type'=>'AIR',
           'seat_type'=>$seat_type,
           'sales_price'=>$sales_price,
           'remark'=>$remark,
        ]);
    }

    public function getList($limit, $page, $order_number, $user_id, $tm, $flight_no, $pnr, $booking_day_start, $booking_day_end, $status, $fly_day_start, $fly_day_end)
    {
        $whereArr = [];

        empty($order_number)|| $whereArr[] = ['o.order_number', 'like', '%'.$order_number.'%'];
        empty($user_id)|| $whereArr[] = ['o.user_id', 'like', '%'.$user_id.'%'];
        empty($tm)|| $whereArr[] = ['o.tm', 'like', '%'.$tm.'%'];
        empty($flight_no)|| $whereArr[] = ['o.flight_no', 'like', '%'.$flight_no.'%'];
        empty($pnr)|| $whereArr[] = ['t.pnr', 'like', '%'.$pnr.'%'];
        empty($booking_day_start)|| $whereArr[] = ['o.create_time', '>', $booking_day_start];
        empty($booking_day_end)|| $whereArr[] = ['o.create_time', '<', $booking_day_end];
        empty($fly_day_start)|| $whereArr[] = ['o.format_fly_day', '>', $fly_day_start];
        empty($fly_day_end)|| $whereArr[] = ['o.format_fly_day', '<', $fly_day_end];
        empty($status)|| $whereArr[] = ['o.status', $status];

        return $this->dao->alias('o')
            ->join('ticket t', 't.order_id = o.id')
            ->join('airplane_passenger p', 't.passenger_id = p.id')
            ->join('users u', 'u.id = o.user_id')
            ->field('o.*,u.username as drawer,t.pnr,t.seat,t.ticket_no,t.pay_price,t.return_cash,t.pnr,p.name,p.id_card,p.card_type,p.is_chd,p.is_inft,p.second_name')
            ->where($whereArr)
            ->order('o.create_time desc')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ]);

    }
    private function getCity($airLines)
    {
        $originInfo = Airports::where('code', $airLines)->find();
        if (empty($originInfo)) {
            $name = $airLines;
        } else {

            $city = City::where('city', 'like', '%'.mb_substr($originInfo['name'], 0 ,2, 'UTF-8').'%')->find();


            if (empty($city)) {
                $name = $originInfo['name'];
            } else {
                $name = $city['city_name'] == $city['province'] ? $city['city_name'] : $city['city_name'] . ',' . $city['province'];
            }
        }
        return $name;
    }

    public function updateStart($order_id, $status){
        return $this->dao->where('id', $order_id)->update(['status'=> $status]);
    }
}